/**
 * 全局变量
 */
var selectUserId = '';
$(function(){
    /*
    * 加载列表数据
    * */
    $('#hdList').datagrid({
        url:ROOT_PATH+'/hiddenDanger/hdGovern/queryHDGovernMonReportList.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        type:"post",
        idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},
            {field:'yearth',title:'年度',width:10,sortable:true,
                formatter:function(value,row,index){
                    if(value){
                        return value+'年';
                    }

                }
            },
            {field:'month',title:'月份',width:10,sortable:true,
                formatter:function(value,row,index){
                    if(value){
                        return value+'月';
                    }

                }
            },
            {field:'name',title:'隐患治理报表',width:10,sortable:true,
                formatter:function(value,row,index){
                    return "<a style='color: deepskyblue' href='javascript:void(0);' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                }
            },
            {field:'addDeptName',title:'登记部门',width:10,sortable:true,
                formatter:function(value,row,index){
                    // var val = row.addDept.name;
                    if(row.addDept){
                        return row.addDept.name;
                    }
                    return '';
                }
            },
            {field:'addUserName',title:'登记人员',width:10,sortable:true,
                formatter:function(value,row,index){
                    // var val = row.addDept.name;
                    if(row.addUser){
                        return row.addUser.name;
                    }
                    return '';
                }
            },
            {field:'addTime',title:'登记时间',width:10,sortable:true},
            {field:'remarks',title:'备注',width:10,sortable:true}
        ]],
        toolbar: '#toolbar'

    });
    /**
     * 加载下拉列表信息
     *
     */
    //queryDepartmentTree();//加载部门选项
    //加载数据字典下拉列表
    //queryDictionaryList("searchGrade",CONSTANT.DictionaryTypeDefConstants.HD_HDGRADE);//隐患等级
    /**
     * 绑定人员选择事件
     */
    $("#searchChargeUser").textbox({
        iconWidth: 22,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                selectUserId ="searchChargeUser";
                add_contractorInfo_func();
            }
        },
        {
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }
        ]
    });
    $("#searchAddUser").textbox({
        iconWidth: 22,
        icons: [
            {
            iconCls: 'icon-add',
            handler: function(e){
                selectUserId ="searchAddUser";
                add_contractorInfo_func();
            }
        },
        {
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }
        ]
    });
    queryYearthList("searchYearth");
    queryMonthList("searchMonth");
    //绑定按钮事件
    $('#btn_advanced_search').on("click", function () {
        $('#searchForm2').toggle();
        $('#hdList').datagrid('resize');
    });
    $("#btn_hd_hdGovern_add").on("click",function(){
        addHDRegister();
    });
    $("#btn_hd_hdGovern_edit").on("click",function(){
        editHDGovern();
    });
    $("#btn_hd_hdGovern_delete").on("click",function(){
        deleteHDGovern();
    });
    //查询按钮绑定事件
    $("#btn_search").on('click',function(){
        searchFun();
    });
    $('#btn_clear').on('click',function(){
        $('#searchForm1').form('clear');
        $('#searchForm2').form('clear');
    });

});
/**
 * 打开添加页面
 */
function addHDRegister () {
    /*var $departmentTree = $('#department_tree');
     var $selectedTreeNode = $departmentTree.tree('getSelected');
     var deptId;
     //未选择时获取根节点
     if ($selectedTreeNode === null) {
     deptId = $departmentTree.tree('getRoot').id;
     }else {
     deptId = $selectedTreeNode.id;
     }*/
    WindowManager.openLayer({
        type: 2,
        title: '新增隐患治理月报',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/hiddenDanger/hdGovern/add.shtml',
        callBack:function (data) {
            $('#hdList').datagrid("reload");
        }
    });
    /*var index = top.layer.open({
        type: 2,
        title: '新增隐患治理月报',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/hiddenDanger/hdGovern/add.shtml',
        end:function(){
            $('#hdList').datagrid("reload");
        }
    });*/
}
/**
 * 打开编辑页面
 */
function editHDGovern(){
    var selectedRow =  $('#hdList').datagrid('getSelected');
    if(selectedRow){
        var businessId = selectedRow.id;
        WindowManager.openLayer({
            type: 2,
            title: '编辑隐患治理月报',
            area: ['1200px', '90%'],
            content: ROOT_PATH + '/hiddenDanger/hdGovern/edit.shtml?businessId='+businessId,
            callBack:function (data) {
                $('#hdList').datagrid("reload");
            }
        });
       /* var index = top.layer.open({
            type: 2,
            title: '编辑隐患治理月报',
            area: ['1200px', '90%'],
            content: ROOT_PATH + '/hiddenDanger/hdGovern/edit.shtml?businessId='+businessId,
            end:function(){
                $('#hdList').datagrid("reload");
            }
        });*/
    }else{
        layer.alert("请选择数据！");
    }

}
/**
 * 打开详情页面
 * @param businessId
 */
function openDetailPage(businessId){
    WindowManager.openLayer({
        type: 2,
        title: '隐患治理月报详情',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/hiddenDanger/hdGovern/detail.shtml?businessId='+businessId,
        callBack:function (data) {
            $('#hdList').datagrid("reload");
        }
    });
   /* var index = top.layer.open({
        type: 2,
        title: '隐患治理月报详情',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/hiddenDanger/hdGovern/detail.shtml?businessId='+businessId,
        end:function(){
            $('#hdList').datagrid("reload");
        }
    });*/
}
/**
 * 删除方法
 */
function deleteHDGovern(){
    var selectedRow =  $('#hdList').datagrid('getSelected');
    if(selectedRow){
        var businessId = selectedRow.id;
        layer.confirm("确定删除该数据？",{btn:['确定','取消']},function(){
            $.ajax({
                url:ROOT_PATH + '/hiddenDanger/hdGovern/delete.shtml',//提交地址
                data:{businessId:businessId},//将表单数据序列化
                type:"POST",
                dataType:"json",
                success:function(result){
                    //console.log(result);
                    if (result.success){
                        layer.msg(result.message,{time:1000,icon:1},function(){
                            $('#hdList').datagrid('reload');
                        });
                    }else{
                        layer.msg(result.message,{time:1000,icon:1},function(){
                        });
                    }
                }
            });
        });

        /*var index = top.layer.open({
            type: 2,
            title: '隐患信息登记',
            area: ['1200px', '90%'],
            content: ROOT_PATH + '/hiddenDanger/hdRegister/edit.shtml?businessId='+businessId,
            end:function(){
                $('#hdList').datagrid("reload");
            }
        });*/
    }else{
        layer.alert("请选择数据！");
    }

}
/**
 * 查询按钮方法
 */
function searchFun(){
    var search1 =$('#searchForm1').serializeJson();
    var search2 =$('#searchForm2').serializeJson();
    var search = $.extend(search1,search2);
    //排序参数
    search.sort = "addTime";
    search.order='desc';
    $('#hdList').datagrid('load',search);
}

/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            //隐患所在部门
            $('#searchDept').combotree(
                {
                    data:result,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );
            //隐患登记部门
            $('#searchAddDept').combotree(
                {
                    data:result,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );

        }
    });
}
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    // console.log(data);
    //给行编辑赋值
    if(selectUserId==""||selectUserId==null){
        var dutyUserED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserName'});
        var dutyUserIdED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserId'});
        $(dutyUserED.target).textbox("setValue",data[0].name)
        $(dutyUserIdED.target).textbox("setValue",data[0].id)
    }else{//给正常文本框赋值
        $("#"+selectUserId).textbox('setValue',data[0].id);
        $("#"+selectUserId).textbox('setText',data[0].name);
        selectUserId = "";
    }

    //console.log();


    //$("#"+selectUserId+"Id").val(data.id);
    // $("#"+selectUserId).textbox('setValue','值123');
    //$("#"+selectUserId).val(data.id);
    // $("#"+selectUserId).text(data.name);
    return data;
};
var add_contractorInfo_func = function () {
    layer.open({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0'
    });
};

/**
 * 查询数据字典列表的方法
 * @param id
 * @param code
 */
function queryDictionaryList(id,code){
    $.ajax({
        url:ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',//提交地址
        data:{code : code},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#'+id).combobox(
                {
                    data:result,
                    valueField:'id',
                    textField:'text',
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        for(var i=0;i<data.length;i++){
                            newData.push(data[i])
                        }
                        return newData;
                    }
                }
            );

        }
    });
}
/**
 * 设置年度下拉列表
 * @param id
 * @param code
 */
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'}
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text'
        }
    );
}
/**
 * 设置月份下拉列表
 * @param id
 */
function queryMonthList(id){
    var monthData = new Array();
    monthData.push({id:"",text:"请选择"});
    for(var i =1;i<=12;i++){
        var m = {id:i,text:i+'月'}
        monthData.push(m);
    }
    $('#'+id).combobox(
        {
            data:monthData,
            valueField:'id',
            textField:'text'
        }
    );
}